<template>
  <div class="container">
    <nearby-wi-fi-nav-bar />
    <u-alert title="请到门店附近连接WiFi" type="warning" center />
    <div class="no-location" v-if="!currentLocation">
      <u-empty
        mode="wifi"
        text="无法获取定位，请手动定位"
        icon="https://cdn.uviewui.com/uview/empty/wifi.png"
      />
      <!--      <text class="text">无法获取定位，请手动定位</text>-->
      <view
        class="_button marginTop-60"
        :style="{ background: themeColor }"
        @click="
          $Router.push({
            name: 'chooseAddress',
          })
        "
        >马上定位</view
      >
    </div>

    <div class="no-location" v-if="currentLocation && list.length === 0">
      <u-empty
        mode="wifi"
        text="附近3km无免费WiFi"
        icon="https://cdn.uviewui.com/uview/empty/wifi.png"
      >
      </u-empty>
      <!--      <text class="text">附近3km无免费WiFi</text>-->
    </div>
    <div
      class="wifi-list paddingTop-26"
      v-if="currentLocation && list.length > 0"
    >
      <div
        v-for="(item, index) in list"
        :key="index"
        class="wifi-list-item flex-align-center"
      >
        <div class="left">
          <div>{{ item.wifiName }}</div>
          <div v-if="item.pubDomainShopName">
            门店名称：{{ item.pubDomainShopName }}
          </div>
        </div>

        <div class="right flex-align-center">
          <div v-if="item.distance" class="text marginRight-10">
            {{ item.distance | forMatDistance }}
          </div>
          <div>
            <view
              class="_button"
              :style="{ background: themeColor }"
              @click="handleLinkWiFi(item)"
              >连接</view
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import nearbyWiFiNavBar from "./components/nearbyWiFiNavBar";
import { mapState } from "vuex";
import { getNearByWifi } from "@/service/api/sp-store/wifi";

export default {
  name: "index",
  components: { nearbyWiFiNavBar },
  computed: {
    ...mapState("sp-address/location", ["currentLocation"]),
    ...mapState("sp-common/base", ["themeColor"]),
  },
  data() {
    return {
      src: "https://cdn.uviewui.com/uview/album/1.jpg",
      list: [],
    };
  },
  filters: {
    forMatDistance(val) {
      return Number(val) > 1000
        ? (Number(val) / 1000).toFixed(2) + "km"
        : val.toFixed(2) + "m";
    },
  },
  onLoad() {},
  watch: {
    currentLocation: {
      handler(val) {
        if (val) {
          this.fetchedData();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    async fetchedData() {
      const { latitude, longitude } = this.currentLocation;
      const data = await getNearByWifi({ latitude, longitude });
      this.list = data?.filter((v) => v.distance <= 3000);
    },
    handleLinkWiFi({ wifiName, password, wifiCode }) {
      this.$Router.push({
        name: "linkWifi",
        params: {
          name: wifiName,
          password,
          deviceNo: wifiCode,
        },
      });
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  background-color: #f8f8f8;
  min-height: 100vh;
}
.no-location {
  width: 95vw;
  text-align: center;
  margin: 80rpx auto 0 auto;
  ._button {
    width: 686rpx;
    height: 80rpx;
    line-height: 80rpx;
    margin: 60rpx auto 10rpx auto;
    text-align: center;
    color: white;
    border-radius: 44rpx;
  }
}
.wifi-list {
  width: 686rpx;
  margin: 0 auto;
  border-radius: 15rpx;
  &-item {
    border-bottom: 1rpx solid #eeee;
    padding: 23rpx 16rpx;
    font-size: 29rpx;
    background-color: white;
  }
  &-item:first-child {
    border-top-right-radius: 15rpx;
    border-top-left-radius: 15rpx;
  }
  &-item:last-child {
    border-bottom-right-radius: 15rpx;
    border-bottom-left-radius: 15rpx;
    border-bottom: none;
  }
  ._button {
    width: 100rpx;
    height: 50rpx;
    line-height: 50rpx;
    text-align: center;
    color: white;
    border-radius: 44rpx;
    font-size: 26rpx;
  }
  .left {
    flex: 1;
  }
}
.text {
  font-weight: 500;
  color: #131313;
  opacity: 0.7;
}
</style>
